package com.kldy8.kldyw.tools;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextUtil {
    /**
     * 校验空值操作
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        return str == null || str.equals("");
    }

    public static boolean isEmpty(CharSequence chars) {
        return chars == null || isEmpty(chars.toString());
    }

    public static String conversion(String tag) {
        String result = "未知";
        try {
            double l = Double.parseDouble(tag);
            if (l < 1000) {
                return tag;
            }

            String d = l / 1000 + "";
            if (d.contains(".")) {
                result = d.substring(0, d.indexOf(".") + 2);
            } else {
                result = d;
            }

        } catch (Exception e) {
            return result;
        }
        return result;
    }

    /**
     * 判断是否全部有字母和数字组成
     *
     * @param name
     * @return
     */
    public static boolean isLimit(String name) {
        // ^.[a-zA-Z]\w{m,n}$ 由m-n位的字母数字或下划线组成
        Pattern p = Pattern.compile("[a-zA-Z0-9_@]*");
        Matcher m = p.matcher(name);
        return m.matches();
    }

    /**
     * 判断是否是电话号码
     *
     * @param mobiles
     * @return
     */
    public static boolean isMobileNumber(String mobiles)
    {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * 判断密码长度是否在6-20位
     * @param password
     * @return
     */
    public static boolean isPasswordLength(String password) {
        Pattern p = Pattern
                .compile("^[a-z0-9_-]{6,20}$");
        Matcher m = p.matcher(password);
        return m.matches();
    }

    public static String getdistance(String dis) {
        int disint = -1;
        try {
            disint = Integer.parseInt(dis);
        } catch (Exception e) {
        }
        if (disint == -1) {
            try {
                disint = (int) Double.parseDouble(dis);
            } catch (Exception e) {
            }
        }
        return getdistance(disint);

    }

    public static String getdistance(float dis) {
        int disint = -1;
        try {
            disint = (int) dis;
        } catch (Exception e) {
        }
        return getdistance(disint);
    }

    /**
     * 从米获取距离
     *
     * @return
     */
    public static String getdistance(int dis) {
        if (dis < 0)
            return "未知";

        String resu;
        if (dis >= 1000) {
            double Kdist = dis / 1000.0;
            resu = String.format("%.2f", Kdist) + "km";// 保留两位小数
        } else {
            resu = dis + "m";// 原始距离
        }

        return resu;
    }

    /**
     * 判断email格式是否正确
     */
    public static boolean isEmail(String email) {
        String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * 判断微信号格式是否正确
     */
    public static boolean isWechat(String wechat) {
        String str = "^[0-9a-zA-Z_]{5,}+$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(wechat);
        return m.matches();
    }

    /**
     * String转换到int
     *
     * @param str
     * @param defvalue 默认文本
     * @return
     */
    public static int String2int(String str, int defvalue) {
        int tem = defvalue;
        try {
            tem = Integer.parseInt(str);
        } catch (Exception e) {
        }
        return tem;
    }

}
